package com.sky.mapper.user;


import com.sky.pojo.Merchant;
import com.sky.pojo.ShoppingCart;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ShoppingCartMapper {
    /**
     * 添加购物车
     * @param
     */
    @Insert("insert into shopping_cart(name,user_id,dish_id, setmeal_id, dish_flavor,number,amount,image,create_time) " +
            "values (#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount}),{image},#{createTime})")
    void add(ShoppingCart shoppingCart);

    //判断套餐中是否存在套餐或者菜品        (根据用户id查询)
    @Select("select * from shopping_cart where user_id=#{userId} and(dish_id=#{dishId} or setmeal_id=#{setmealId}) ;")
    ShoppingCart getByDishSetmeal(Long dishId, Long setmealId, Long userId);

    /**
     * 更新份数+1
     * @param
     * @param
     */
    @Update("update shopping_cart set number=#{number} where id=#{ScId};")
    void updeteNumById(int number, Long ScId);
    /**
     * 更改口味
     * @param dishFlavor
     * @param id
     */
    @Update("UPDATE shopping_cart SET dish_flavor = #{dishFlavor} where id=#{id}")
    void updetedishFlavor(String dishFlavor, Long id);

    /**
     * 插入购物车数据
     * @param shoppingCart
     */
    @Insert("insert into shopping_cart(name,user_id,dish_id, setmeal_id, dish_flavor,number,amount,image,create_time) " +
            "values (#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{image},#{createTime})")
    void addshoppingCaret(ShoppingCart shoppingCart);

    /**
     * 查询购物车
     * @return
     */
    @Select("select  name, image, dish_id dishId, setmeal_id setmealId, dish_flavor dishFlavor, number, amount, create_time createTime from shopping_cart " +
            "where user_id=#{currentId}")
    List<ShoppingCart> getShopping(Long currentId);


    /**
     * 清空购物车   根据购物车id删除
     * @param currentId
     */
@Update("delete from shopping_cart where user_id=#{currentId};")
    void deleteByUserId(Long currentId);


    /**
     * 计算配送地址和商家地址之间的配送时间
     * @param currentId
     * @return
     */
    Integer getMakeTime(Long currentId);

    /**
     * 查询商家地址
     * @param shopId
     * @return
     */
    Merchant getById(Integer shopId);
}
